<?php
class SiteAction extends MemberAction{
    //出售一个站点
	
	
	
	public function create(){
		if(isset($_POST['goods_id']) || isset($_COOKIE['createsite'.__SITEID__])){
			$orderDB = D('Order');
			if(isset($_COOKIE['createsite'.__SITEID__]) && !isset($_POST['goods_id'])){
				$dataarr = explode('-', $_COOKIE['createsite'.__SITEID__]);
				$_POST['goods_id'] = $dataarr[0];
				$_POST['num'] = $dataarr[1];
				setcookie('createsite'.__SITEID__, NULL, time() - 1, '/');
			}
			if(!$_POST['goods_id'] || !$this->memberInfo['id']){
				return false;
			}
			$Product = M('Product') -> where(array('id' => $_POST['goods_id'])) -> find();
			if($Product['systemagent'] == 0){
				return false;
			}
			$Agentpricedb = D('Agentprice');
			if(intval($_POST['num']) == 0){
				$_POST['num'] = 0;
			}else{
				$_POST['num'] = abs(intval($_POST['num']));
			}
			$_POST['maxNum']=isset($_POST['maxNum'])?$_POST['maxNum']:0;
			
			$price = $Agentpricedb -> getprice($Product['systemagentprice'], $_POST['num']);
			
			
			$aff = $Agentpricedb -> getaff($Product['systemagentprice'], $_POST['num']);
			
			
			$_POST['siteid'] = $Product['systemagent'];
			$_POST['site_id'] = __SITEID__;
			$_POST['relate_id'] = __RELATEID__;
			$_POST['status'] = 1;//0.已取消 1.待支付 2.已支付
			$_POST['ordernumber'] = build_order_no();
			$_POST['create_time'] = time();
			$_POST['create_date'] = strtotime(date('Y-m-d'));
			//$_POST[C('TOKEN_ON')] = $_POST[C('TOKEN_ON')];
			$_POST['total'] = $price * $_POST['num'];
			
			$_POST['member_id'] = isset($this->memberInfo['id'])?$this->memberInfo['id']:0;
			$_POST['pay_type'] = isset($_POST['pay_type'])?$_POST['pay_type']:'';
			$maxNumStr=isset($_POST['maxNum'])?$_POST['maxNum'].'人公司':'';
			if($_POST['num'] > 0){
			    $_POST['Guestbook'] = '开通【'.$Product['title'].'】【'.$_POST['num'].'个月】'.$maxNumStr;
			}else{
				$_POST['Guestbook'] = '开通【'.$Product['title'].'】【终身版】'.$maxNumStr;
			}
			$membersaledb = M('Membermembersale');

			$member_memberdb = M('Member_member');
			//$orderDB->create();
			//$saledata['payment_id'] = intval($_POST['payment_id']);
			$orderId = $orderDB->add($_POST);
			if($orderId){ 
				//写入订单明细
				$saledb = M('Sale');
				$saledata = array();
				$saledata['order_id'] = $orderId;
				$saledata['product_id'] = $Product['id'];
				
				$saledata['member_id'] = $_POST['member_id'];
				$saledata['num'] = $_POST['num'] == 0 ? 1 : $_POST['num'];
				$saledata['site_id'] = __SITEID__;
				$saledata['price'] = $price;
				$saledata['aff_money'] = $aff; 
				$saledata['total'] = $_POST['total'];
				$saledata['attr'] = '';
				$saledata['title'] = $Product['title'];
				$saledata['img_url'] = $Product['img_url'];
				$saledata['supplier_id'] = 0;
				$saledata['code'] = $v['code'];
				$sale_id = $saledb -> data($saledata) -> add();
				
				
				
				//写入销售记录与推荐人关系表
					//下单的人所有上家
					$memberpids = $member_memberdb -> where(array('smember_id' => $saledata['member_id'])) -> select();
					foreach($memberpids as $mk => $mv){
						$membersale['order_id'] = $orderId;
						$membersale['site_id'] = __SITEID__;
						$membersale['sale_id'] = $sale_id;
						$membersale['product_id'] = 0;
						$membersale['dateline'] = $_POST['create_time'];
						$membersale['member_id'] = $mv['member_id'];
						$membersale['deep'] = $mv['deep'];
						$membersale['price'] = $saledata['price'];
						$membersale['aff_money'] = $saledata['aff_money'];
						$membersale['num'] = $saledata['num'];
						$membersale['total'] = $saledata['total'];
						
						$membersaledb -> add($membersale); 
					}
				
				
				
				
				
				if($_POST['total'] == 0){
					D('Order') -> upstate($_POST['ordernumber']);
					
				    redirect(U('Payment/index', array('id' => $orderId, 'payment_id' => intval($_POST['payment_id']))));
				}
				C('URL_MODEL',0);
				if(IS_AJAX){
					$this->success('成功创建订单！', U('Home/Payment/index', array('id' => $orderId, 'payment_id' => intval($_POST['payment_id'])),true,false,true));
				}else{
					redirect(U('Home/Payment/index', array('id' => $orderId, 'payment_id' => intval($_POST['payment_id'])),true,false,true));
				}
				
			}else{
				$this->error($orderDB->getError());
			}
		}else{
			$list = M('Site') -> where(array('price' => array('neq', ''))) -> select();
			$this->assign('list', $list);
			$this->display();
		}
	}
}